package com.wondersgroup.insurance.dnuilearn.views.canvas;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator on 2017/5/22.
 */

public class SearchView extends View {

    Paint mPaint = new Paint();
    int cX, cY;
    final int  radius = 30;

    Path mPath =null;

    double sin45= 0;

    RectF reactF = new RectF();

    int lineWidth  = 0;

    final int padding = 100;

    int ratio = 0;
    public SearchView(Context context) {
        this(context, null);
    }

    public SearchView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SearchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(6);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPath = new Path();

        sin45 = Math.sin(45);

        handler.sendEmptyMessageDelayed(100,5000);
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            startAnimation();
        }
    };

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        cX = w/2;
        cY = h/2;

        lineWidth = w - padding*2;
        reactF.set(cX-radius,cY-radius,cX+radius,cY+radius);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.GREEN);
//        canvas.draw
        mPath.reset();

//        mPath.addArc(reactF,45,-180);
//        canvas.drawPath(mPath,mPaint);
//        mPath.addCircle(cX,cY,radius, Path.Direction.CW);
        int delta = (int) (sin45*radius);
        canvas.save();
        canvas.translate((float) (ratio*(lineWidth - 4 *radius*sin45)/200.0f),0);

         if (ratio <25){
            mPath.addArc(reactF,45,(4*ratio/100.0f-1.0f)*360);
            mPath.moveTo(cX +delta, cY+ delta);
            mPath.lineTo(cX +delta*2, cY+ delta*2);
             mPath.lineTo(cX +delta*2 - ratio*lineWidth/100.0f, cY+ delta*2);

        }else if (ratio < 50){
             int deltaHandle = (int) (delta*(1+(ratio -25)/25.0f));
             mPath.moveTo(cX +deltaHandle, cY+ deltaHandle);
             mPath.lineTo(cX +delta*2, cY+ delta*2);
         }else {
             mPath.moveTo(cX +delta*2, cY+ delta*2);
         }
        mPath.lineTo(cX +delta*2 - ratio*lineWidth/100.0f  , cY+ delta*2);
        canvas.drawPath(mPath,mPaint);
        canvas.restore();


    }



    public void startAnimation(){
        ValueAnimator valueAnimator = null;
        if (ratio == 0){
            valueAnimator = ValueAnimator.ofInt(0,100);
        }else {
            valueAnimator = ValueAnimator.ofInt(100,0);

        }

        valueAnimator.setDuration(8000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                ratio  = (int) animation.getAnimatedValue();
                invalidate();
            }


        });
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                handler.sendEmptyMessageDelayed(100,2000);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        valueAnimator.start();
    }
}
